﻿using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

namespace PlanningGIS.ArcGIS
{
    /// <summary>
    /// 
    /// </summary>
    public class RendererHelper
    {
        /// <summary>
        /// 设置图层颜色
        /// </summary>
        /// <param name="featLyr"></param>
        /// <param name="color"></param>
        public static void SetSymbol(IFeatureLayer featLyr, IColor color)
        {
            ISimpleRenderer simpleRenderer = new SimpleRenderer();
            IGeoFeatureLayer geoFeatLyr = featLyr as IGeoFeatureLayer;
            ISymbol symbol = null;
            switch (featLyr.FeatureClass.ShapeType)
            {
                case esriGeometryType.esriGeometryPoint:
                    symbol = new SimpleMarkerSymbol() as ISymbol;
                    IMarkerSymbol markerSymbol = symbol as IMarkerSymbol;
                    markerSymbol.Color = color;
                    break;
                case esriGeometryType.esriGeometryPolyline:
                    symbol = new SimpleLineSymbol() as ISymbol;
                    ILineSymbol lineSymbol = symbol as ILineSymbol;
                    lineSymbol.Color = color;
                    break;
                case esriGeometryType.esriGeometryPolygon:
                    symbol = new SimpleFillSymbol() as ISymbol;
                    IFillSymbol fillSymbol = symbol as IFillSymbol;
                    fillSymbol.Color = color;
                    break;
            }
            simpleRenderer.Symbol = symbol;
            geoFeatLyr.Renderer = simpleRenderer as IFeatureRenderer;
        }

        /// <summary>
        /// 创建唯一值渲染，使用色阶进行渲染
        /// </summary>
        /// <param name="fieldname">字段名称</param>
        /// <param name="values">唯一值：支持IList对象</param>
        /// <param name="fromcolor">开始颜色</param>
        /// <param name="tocolor">结束颜色</param>
        /// <returns></returns>
        public static IFeatureRenderer CreateUniqueValueRenderer(string fieldname, IList values, IRgbColor fromcolor = null, IRgbColor tocolor = null)
        {
            if (values == null || values.Count == 0) return null;
            int count = values.Count;
            if (fromcolor == null)
            {
                fromcolor = new RgbColor();
                fromcolor.RGB = ColorTranslator.ToOle(Color.Green);
            }
            ISimpleFillSymbol defaultsymbol = new SimpleFillSymbol();
            defaultsymbol.Color = fromcolor;
            if (count == 1)
            {
                ISimpleRenderer renderer = new SimpleRenderer();
                renderer.Label = values[0].ToString();
                renderer.Symbol = defaultsymbol as ISymbol;
                return (IFeatureRenderer)renderer;
            }
            else
            {
                IUniqueValueRenderer renderer = new UniqueValueRenderer();
                renderer.FieldCount = 1;
                renderer.set_Field(0, fieldname);
                renderer.DefaultSymbol = defaultsymbol as ISymbol;
                renderer.UseDefaultSymbol = true;
                IAlgorithmicColorRamp colorRamp = new AlgorithmicColorRamp();
                colorRamp.Size = count;
                colorRamp.FromColor = fromcolor;
                if (tocolor == null)
                {
                    tocolor = new RgbColor();
                    tocolor.RGB = ColorTranslator.ToOle(Color.Red);
                }
                colorRamp.ToColor = tocolor;
                colorRamp.Algorithm = esriColorRampAlgorithm.esriHSVAlgorithm;
                IColorRamp colorramp = (IColorRamp)colorRamp;
                colorramp.Size = count;
                bool createRamp = false;
                colorramp.CreateRamp(out createRamp);
                IEnumColors enumColors = colorramp.Colors;
                enumColors.Reset();
                foreach (object v in values)
                {
                    ISimpleFillSymbol fsymbol = new SimpleFillSymbol();
                    fsymbol.Color = enumColors.Next();
                    renderer.AddValue(v.ToString(), "", fsymbol as ISymbol);
                }
                return (IFeatureRenderer)renderer;
            }

        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="featureLayer"></param>
        /// <param name="fieldName"></param>
        /// <param name="numClasses"></param>
        /// <param name="classificationMethod"></param>
        /// <param name="maxSample"></param>
        /// <returns></returns>
        public static double[] NaturalBreak(IFeatureLayer featureLayer, string fieldName, ref int numClasses, string classificationMethod, int maxSample)
        {
            ITableHistogram tableHistogram = new BasicTableHistogramClass();
            IGeoFeatureLayer geoFeatureLayer = (IGeoFeatureLayer)featureLayer;
            ITable pTable = (ITable)geoFeatureLayer;
            tableHistogram.Field = fieldName;
            tableHistogram.Table = pTable;
            //pTableHistogram.Sampling.MaxSampleSize = MaxSample;
            object dataFrequency = null;
            object dataValues = null;
            IBasicHistogram basicHistogram = (IBasicHistogram)tableHistogram;
            basicHistogram.GetHistogram(out dataValues, out dataFrequency);

            IClassifyGEN classify = null;
            switch (classificationMethod)
            {
                case "Natural Breaks (Jenks)":
                    classify = new NaturalBreaksClass();
                    break;
                case "Equal Interval":
                    classify = new EqualIntervalClass();
                    break;
                case "Defined Interval":
                    classify = new DefinedIntervalClass();
                    break;
                case "Quantile":
                    classify = new QuantileClass();
                    break;
                case "Standard Deviation":
                    classify = new StandardDeviationClass();
                    break;
                case "Geometrical Interval":
                    classify = new GeometricalIntervalClass();
                    break;
            }

            if (classify == null) return null;
            int count = numClasses;
            if (classificationMethod == "Defined Interval")
            {
                IIntervalRange2 IntervalRange = classify as IIntervalRange2;
                double range = IntervalRange.get_Default(dataValues, dataFrequency);
                IntervalRange.IntervalRange = range;
                classify.Classify(dataValues, dataFrequency, ref numClasses);
            }
            else if (classificationMethod == "Standard Deviation")
            {
                IStatisticsResults sta = tableHistogram as IStatisticsResults;
                IDeviationInterval DeviationInterval = classify as IDeviationInterval;
                DeviationInterval.DeviationInterval = 1;
                DeviationInterval.Mean = sta.Mean;
                DeviationInterval.StandardDev = sta.StandardDeviation;
                classify.Classify(dataValues, dataFrequency, ref numClasses);
            }
            else
            {
                classify.Classify(dataValues, dataFrequency, ref numClasses);
            }
            double[] doubleArray = classify.ClassBreaks as double[];
            return doubleArray;
        }

    }
}
